package com.bms.action;

import com.bms.dao.UserDao;
import com.bms.entity.User;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserAction extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进入post方法");
		UserDao dao = new UserDao();

		// 1、先获取暗号
		// 返回的是表单中name的值
		String method = request.getParameter("method");
		System.out.println("将进行的操作是：" + method);
		// 2、对暗号
		if ("add".equals(method)) {// 添加
			String userName = request.getParameter("userName");
			String realName = request.getParameter("realName");
			String password = request.getParameter("password");

			// 对密码加密处理
			password = DigestUtils.md5Hex(password);

			User temp = new User(userName, realName, password);

			// 保存用户数据
			dao.insert(temp);

			// 跳转到作者主页面
			response.sendRedirect("users/index.jsp");
		} else if ("delete".equals(method)) {// 删除
			// 获取提交的id的值
			String param = request.getParameter("id");
			int id = Integer.valueOf(param);
			dao.deleteUserById(id);
			response.sendRedirect("users/index.jsp");
		} else if ("login".equals(method)) {// 登录
			// 获取用户名和密码
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			// 将密码加密
			//password = DigestUtils.md5Hex(password);
			User param = new User();
			param.setUserName(userName);
			param.setPassword(password);
			// 查询数据库之后返回的user对象
			User user = dao.queryUser(param);
			// 判断用户有没有
			if (user != null) {
				// 1、先获取session对象
				HttpSession session = request.getSession();
				// 2、存放登录用户
				session.setAttribute("user", user);
				response.sendRedirect("users/index.jsp");
			} else {
				// 提示
				// 1、先将提示信息request
				request.setAttribute("error", "登录失败！用户名或密码错误！");
				// 2、重新返回登录界面-->给用户提示
				request.getRequestDispatcher("login.jsp").forward(request,
						response);
			}
		} else if ("logout".equals(method)) {// 退出
			// 1、将session中的数据清空(登录的时候存入用户)
			HttpSession session = request.getSession();
			// 清空session中的所有的数据
			session.invalidate();
			// 2、跳转到登录界面
			response.sendRedirect("login.jsp");
		} else if ("doEdit".equals(method)) {// 修改
			System.out.println("开始doedit操作");
			String paramId = request.getParameter("id");
			int id = Integer.valueOf(paramId);

			String realName = request.getParameter("realName");
			String password = request.getParameter("password");
			// 作为密码修改的标识
			String md5flag = request.getParameter("md5flag");
			// 说明密码进行了修改
			if (StringUtils.isNoneBlank(md5flag) && "1".equals(md5flag)) {
				password = DigestUtils.md5Hex(password);
			}
			// 更新数据库
			User temp = new User();
			temp.setId(id);
			temp.setRealName(realName);
			temp.setPassword(password);
			dao.updateUser(temp);
			response.sendRedirect("users/index.jsp");
		}
	}

}
